/*!
 * @file moduleKeyboard.cpp
 * @brief Ciało moduleKeyboard.hh
 */

#include "engine/key/moduleKeyboard.hh"
using namespace std;

/*--------------------------------------------------------------------------*/

moduleKeyboard::moduleKeyboard() :
	moduleBase("keyboard") {

	this->addKey(new key(SDLK_1, k_1));
	this->addKey(new key(SDLK_2, k_2));
	this->addKey(new key(SDLK_3, k_3));
	this->addKey(new key(SDLK_4, k_4));
	this->addKey(new key(SDLK_5, k_5));
	this->addKey(new key(SDLK_6, k_6));
	this->addKey(new key(SDLK_7, k_7));
	this->addKey(new key(SDLK_8, k_8));
	this->addKey(new key(SDLK_9, k_9));
	this->addKey(new key(SDLK_0, k_0));

	this->addKey(new key(SDLK_q, k_q));
	this->addKey(new key(SDLK_w, k_w));
	this->addKey(new key(SDLK_e, k_e));
	this->addKey(new key(SDLK_r, k_r));
	this->addKey(new key(SDLK_t, k_t));
	this->addKey(new key(SDLK_y, k_y));
	this->addKey(new key(SDLK_u, k_u));
	this->addKey(new key(SDLK_i, k_i));
	this->addKey(new key(SDLK_o, k_o));
	this->addKey(new key(SDLK_p, k_p));

	this->addKey(new key(SDLK_a, k_a));
	this->addKey(new key(SDLK_s, k_s));
	this->addKey(new key(SDLK_d, k_d));
	this->addKey(new key(SDLK_f, k_f));
	this->addKey(new key(SDLK_g, k_g));
	this->addKey(new key(SDLK_h, k_h));
	this->addKey(new key(SDLK_j, k_j));
	this->addKey(new key(SDLK_k, k_k));
	this->addKey(new key(SDLK_l, k_l));

	this->addKey(new key(SDLK_z, k_z));
	this->addKey(new key(SDLK_x, k_x));
	this->addKey(new key(SDLK_c, k_c));
	this->addKey(new key(SDLK_v, k_v));
	this->addKey(new key(SDLK_b, k_b));
	this->addKey(new key(SDLK_n, k_n));
	this->addKey(new key(SDLK_m, k_m));
	this->addKey(new key(SDLK_m, k_m));

	this->addKey(new key(SDLK_SPACE, k_space));

	this->addKey(new key(SDLK_LSHIFT, k_lshift));
	this->addKey(new key(SDLK_LALT, k_lalt));
	this->addKey(new key(SDLK_LCTRL, k_lctrl));

	this->addKey(new key(SDLK_RSHIFT, k_rshift));
	this->addKey(new key(SDLK_RALT, k_ralt));
	this->addKey(new key(SDLK_RCTRL, k_rctrl));

	this->addKey(new key(SDLK_RETURN, k_enter));
	this->addKey(new key(SDLK_BACKSPACE, k_backspace));
	this->addKey(new key(SDLK_TAB, k_tab));
	this->addKey(new key(SDLK_BACKQUOTE, k_console));

	this->addKey(new key(SDLK_UP, k_up));
	this->addKey(new key(SDLK_DOWN, k_down));
	this->addKey(new key(SDLK_LEFT, k_left));
	this->addKey(new key(SDLK_RIGHT, k_right));

	this->addKey(new key(SDLK_PAGEUP, k_pageup));
	this->addKey(new key(SDLK_PAGEDOWN, k_pagedown));

}

/*--------------------------------------------------------------------------*/

bool moduleKeyboard::isPressedC(_keys k) {
	for (list<key*>::iterator i = this->keyList.begin(); i != this->keyList.end(); ++i) {
		if ((*i)->pawirrkey == k) {
			return (*i)->status;
		}
	}
	return false;
}

/*--------------------------------------------------------------------------*/

bool moduleKeyboard::isPressed(_keys k) {
	bool t;
	for (list<key*>::iterator i = this->keyList.begin(); i != this->keyList.end(); ++i) {
		if ((*i)->pawirrkey == k) {
			t = (*i)->status;
			(*i)->status = false;
		}
	}
	return t;
}

/*--------------------------------------------------------------------------*/

void moduleKeyboard::checkInput() {
	SDL_Event event;

	while (SDL_PollEvent(&event)) {

		if (event.type == SDL_KEYDOWN) {

			for (list<key*>::iterator i = this->keyList.begin(); i != this->keyList.end(); ++i) {
				if ((*i)->sdlkey == event.key.keysym.sym) {
					(*i)->status = true;
				}
			}

		} else if (event.type == SDL_KEYUP) {

			for (list<key*>::iterator i = this->keyList.begin(); i != this->keyList.end(); ++i) {
				if ((*i)->sdlkey == event.key.keysym.sym) {
					(*i)->status = false;
				}
			}
		}
	}
	int x, y;
	SDL_GetMouseState(&x, &y);
	this->mousePos = point2f((float) x, (float) y);
}

/*--------------------------------------------------------------------------*/

void moduleKeyboard::addKey(key *nk) {
	this->keyList.push_back(nk);
}

/*--------------------------------------------------------------------------*/

key::key(int sdlkey, _keys pawirrkey) :
	sdlkey(sdlkey), pawirrkey(pawirrkey), status(false) {
}

/*--------------------------------------------------------------------------*/
